// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {GameVar} from "../game/GameVar";
import {BE_KILL_BOARD_COUNT} from "../config/config";
import {wxUtils} from "../wx/wxUtils";
import {frameUpdate} from "../frame/FrameUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BeKilledBoard extends cc.Component {

  @property(cc.Label)
  private labelName: cc.Label = null;

  @property(cc.Label)
  private labelCount: cc.Label = null;

  @property(cc.ProgressBar)
  private progressBar: cc.ProgressBar = null;

  private _cd = 3;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
  }

  private callback: Function;

  show(name: string, callback?: Function) {
    if (GameVar.gameEnd) {
      return;
    }
    this.labelName.string = name;
    this.enabled = true;
    this.node.active = true;
    this.cd = BE_KILL_BOARD_COUNT;
    this.callback = callback;
    wxUtils.showReviveAd();
  }


  set cd(value: number) {
    this._cd = value;
    this.labelCount.string = Math.round(this._cd).toString()
    this.progressBar.progress = this._cd / BE_KILL_BOARD_COUNT
  }

  @frameUpdate()
  updateFixedFrame(dt) {
    this.cd = this._cd - dt;
    if (this._cd <= 0) {
      this.onEnd();
    }
  }

  onEnd() {
    this.enabled = false;
    this.node.active = false;
    wxUtils.hideMenuAd();
    if (this.callback) {
      this.callback()
    }
  }
}
